<?php
/**
 * Created by PhpStorm.
 * User: Zhanghaiqiang
 * Date: 2020-01-28
 * Time: 22:24
 */

namespace App\Core\Endpoints\CandyContent;


use Endpoints\Base\Endpoint;
use Symfony\Component\Console\Input\InputArgument;

class UploadImages extends  Endpoint
{

    // 定义参数
    const ARGUMENT_MINI_PROGRAM_DATA   = 'data';


    // 收参
    public function getArguments()
    {
        return [
            [static::ARGUMENT_MINI_PROGRAM_DATA,InputArgument::OPTIONAL,'数据',''],
        ];
    }
    /**
     * @return mixed
     */
    public function dryRun()
    {
        $data = $this->argument(static::ARGUMENT_MINI_PROGRAM_DATA);
        \Log::debug("upload_debug",[$data]);
        //获取文件name值
        $file = $data['file'];
        if($file->isValid()){
            $ext = $file->getClientOriginalExtension();
            $file_name = date("YmdHis", time()).'-'.uniqid().'.'.$ext;
            if(!in_array($ext, ['jpg', 'png', 'jpeg', 'gif'])) return response()->json(err('文件类型不是图片'));
            //记得创一个文件夹放文件,我的是circle_post_img
            $path = public_path().DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'circle_post_img'.DIRECTORY_SEPARATOR.date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
            $bool =  $file->move($path,$file_name);
            \Log::debug("upload_debug",[$path,$bool]);
            if($bool){
                $img_path = '/uploads/circle_post_img/'.date('Y').'/'.date('m').'/'.date('d').'/'.$file_name;
                $data = [
//                    'domain_img_path'=>get_domain().$img_path,
                    'img_path'=>$img_path,
                ];
                //返回图片路径
                return $data;
            }else{
                return '获取图片路径失败';
            }
        }else{
            return '图片上传失败';
        }
    }
}